package org.jash.demo2205.utils

import android.content.Context
import android.content.SharedPreferences

class SPUtils private constructor(context: Context){
    private val sp: SharedPreferences
    init {
        sp = context.getSharedPreferences("shop",Context.MODE_PRIVATE)
    }
    companion object{
        private var _instance:SPUtils? = null
        val instance:SPUtils
            get() {
                if (_instance==null) {
                    throw RuntimeException("SPUtils使用前要初始化")
                }
                return _instance!!
            }
        fun init(context: Context){
            _instance = SPUtils(context)
        }
    }
    fun putString(key:String,value:String){
        sp.edit().putString(key,value).apply()
    }
    fun getString(key:String,defVal:String):String = sp.getString(key,defVal)?:defVal

    fun putStringSet(key: String, value: Set<String>) {
        sp.edit().putStringSet(key, value).apply()
    }
    fun getStringSet(key: String, defVal:Set<String>):Set<String> = sp.getStringSet(key, defVal) ?: defVal
}